package com.zhihaoliang.jobschedulertrain

import android.os.Handler
import android.os.Message
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit


/**
 * 创建日期：2020/3/21
 * 描述:消息的处理
 * 作者:支豪亮
 */
class InComingMsgHandler(activity: MainActivity) : Handler() {
    private val mainActivity: WeakReference<MainActivity> = WeakReference(activity)

    override fun handleMessage(msg: Message) {
        val mainActivity = mainActivity.get() ?: return
        when (msg.what) {
            //任务开始并在5秒后结束任务
            MSG_COLOR_START -> {
                mainActivity.startJub(msg.what, "开始执行")
                sendMessageDelayed(obtainMessage(MSG_UNCOLOR_START), TimeUnit.SECONDS.toMillis(5))
            }
            MSG_COLOR_STOP -> {
                mainActivity.stopJub(msg.what, "任务完成")
                sendMessageDelayed(obtainMessage(MSG_UNCOLOR_STOP), TimeUnit.SECONDS.toMillis(5))
            }
            //清除开始状态
            MSG_UNCOLOR_START -> {
                mainActivity.clearState(MSG_UNCOLOR_START)
            }
            //清除结束状态
            MSG_UNCOLOR_STOP -> {
                mainActivity.clearState(MSG_UNCOLOR_STOP)
            }

        }
    }
}